package servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import custom.Question;
import custom.Quiz;
import custom.User;

/**
 * Servlet implementation class QuizCreatePushedServlet
 */
@WebServlet("/QuizCreatePushedServlet")
public class QuizCreatePushedServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QuizCreatePushedServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String desc = request.getParameter("desc");
		
		RequestDispatcher dispatch = null;
		if(name == null || desc == null){
			dispatch = request.getRequestDispatcher("newQuiz.jsp");
		}else if(name.length()*desc.length() == 0){
			request.getSession().setAttribute("Note", "Please fill both fields");
			dispatch = request.getRequestDispatcher("newQuiz.jsp");
		} else
			try {
				if( Quiz.checkQuiz(name) ){
					request.getSession().setAttribute("Note", "A Quiz with simmilar name allready exists");
					dispatch = request.getRequestDispatcher("newQuiz.jsp");
				}else{
					request.getSession().setAttribute("questions", new ArrayList<Question>());
					request.getSession().setAttribute("quizName", name);
					request.getSession().setAttribute("quizDesc", desc);
					dispatch = request.getRequestDispatcher("editQuiz.jsp");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		dispatch.forward(request, response);
	}

}
